//承运商主营线路管理

interface ICarRouteManagentViewModel extends IBaseViewModel<ICarRouteManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<RouteLineListItemResponse>;
    /**
     * 起始地省数据下拉
     */
    provinceList:lineAddress[];
    /**
     * 起始地市数据下拉
     */
    startCityList:lineAddress[];
    /**
     * 目的地市数据下拉
     */
    endCityList:lineAddress[];
    /**
     * 起始地省
     */
    startProvinceCode: string;
    /**
     * 起始地市
     */
    startCityCode: string;
    /**
     * 目的地省
     */
    endProvinceCode: string;
    /**
     * 目的地市
     */
    endCityCode: string;
    /**
     * 改变起始地省
     */
    changeStarProvince:()=>void;
    /**
     * 改变起始地市
     */
    changeStartCity:() => void;
    /**
     * 改变目的地省
     */
    changeEndProvince:()=>void;
    /**
     * 改变目的地市
     */
    changeEndCity:() => void;
    /**
     * 添加主营线路
     */
    addRouteLine: () => void;
    /**
     * 删除主营线路
     */
    deleteRouteLine: (id: string) => void;

    disabled:boolean;
}

interface ICarRouteManagentQueryTerms {

}

class CarrierRouteManagentController extends ListController<ICarRouteManagentViewModel, ICarRouteManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<RouteLineListItemResponse>;
    id: string;

    constructor($scope: ICarRouteManagentViewModel,private areaService:IAreaService, private routeLineService: IRouteLineService, private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        super($scope);
        /**
         * 承运商id
         */
        this.init();
    }

    init(): void {
        super.init();
        /**
         * 承运商id
         */
        this.id = JSON.parse(window.localStorage.getItem("carrier").toString()).carrierId;
        if(!this.$scope.required){
            this.$scope.startProvinceCode="-1";
            this.$scope.startCityCode = "-1";
            this.$scope.endProvinceCode = "-1";
            this.$scope.endCityCode = "-1";
        };
        // this.$scope.disabled=$("#carrierFrom").valid();
        /**
         * 获取省下拉
         */
        this.areaService.getArea().then(res =>{
            this.$scope.provinceList = res.data.data;
        });
        this.$scope.changeStarProvince = this.changeStartProvince;
        this.$scope.changeStartCity = this.changeStartCity;
        this.$scope.changeEndProvince = this.changeEndProvince;
        this.$scope.changeEndCity = this.changeEndCity;

        this.$scope.addRouteLine = this.addRouteLine;
        this.$scope.deleteRouteLine = this.deleteRouteLine;
        this.$scope.gridOptions = {
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "承运起始地", field: 'beginAddress', enableColumnMenu: false },
                { displayName: "承运目的地", field: 'endAddress', minWidth: 300, enableColumnMenu: false },
                { displayName: "生成日期", field: 'createTime', enableColumnMenu: false },
                {
                    displayName: "操作",
                    field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: ' <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteRouteLine(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
    }

    loadData(queryTerms: ICarRouteManagentQueryTerms, skip: number, count: number): void {
        this.routeLineService.getList(null, this.id, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            this.$scope.gridOptions.data = result.data.data;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }

            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 改变起始地省
     */
    changeStartProvince = ()=>{
        if(!this.$scope.startProvinceCode||this.$scope.startProvinceCode ==""){
            this.$scope.startCityList.length=0;
            if(!this.$scope.required){
                this.$scope.startProvinceCode="-1";
                this.$scope.startCityCode = "-1";
            };
            return;
        };
        this.areaService.getCity(this.$scope.startProvinceCode).then(res=>{
            this.$scope.startCityList = res.data.data;
        });
        this.able()
    }
    /**
     * 改变起始地市
     */
    changeStartCity = ()=>{
        if(!this.$scope.startCityCode||this.$scope.startCityCode ==""){
            if(!this.$scope.required){
                this.$scope.startCityCode="-1";
            };
        };
        this.able()
    }

    /**
     * 改变目的地省
     */
    changeEndProvince = ()=>{
        if(!this.$scope.endProvinceCode||this.$scope.endProvinceCode ==""){
            this.$scope.endCityList.length=0;
            if(!this.$scope.required){
                this.$scope.endProvinceCode = "-1";
                this.$scope.endCityCode = "-1";
            };
            return;
        };
        this.areaService.getCity(this.$scope.endProvinceCode).then(res=>{
            this.$scope.endCityList = res.data.data;
        });
        this.able()
    }
    /**
     * 改变起始地市
     */
    changeEndCity = ()=>{
        if(!this.$scope.endCityCode||this.$scope.endCityCode ==""){
            if(!this.$scope.required){
                this.$scope.endCityCode="-1";
            };
        };
        this.able()
    }

    able=()=>{
        if(!$("select[name='startProvince']").val()){return}
        if(!$("select[name='startCity']").val()){return}
        if(!$("select[name='popupProvince']").val()){return}
        if(!$("select[name='popupCity']").val()){return}
        
       
       document.getElementById('addRouteLine')['disabled']=false;
        
    }
    /**
     * 删除主营线路
     */
    deleteRouteLine = (id: string) => {
        this.$ngBootbox.confirm("是否删除该数据？").then(e => {
            this.routeLineService.delete(id, "", this.id).then(result => {
                this.reload();
            });
        });
    }

    
    /**
     * 添加主营线路
     */
    addRouteLine = () => {
        
       

        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.routeLineService.add(
                this.$scope.startProvinceCode,
                this.$scope.startCityCode,
                this.$scope.endProvinceCode,
                this.$scope.endCityCode,
                this.id,
                null
            ).then(result => {
                this.reload();
            });
        });
    }
}

angular.module("tms").controller("carrierRouteManagentController", ["$scope","areaService", "routeLineService", "$ngBootbox", "$location", CarrierRouteManagentController]);